<template>
    <view class="uni-navbar">
        <view
            :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--border': border }"
            :style="{ 'background': backgroundColor, 'box-shadow': shadow }"
            class="uni-navbar__content"
        >
            <view class="status-bar" />
            <view
                :style="{ color: color, background: backgroundColor }"
                class="uni-navbar__header uni-navbar__content_view"
            >
                <view
                    @click="onClickLeft"
                    class="uni-navbar__header-btns uni-navbar__header-btns-left uni-navbar__content_view"
                >
                    <view class="uni-navbar__content_view" v-if="leftIcon.length">
                        <icons :color="color" :type="leftIcon" size="24" />
                    </view>
                    <view
                        :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length }"
                        class="uni-navbar-btn-text uni-navbar__content_view"
                        v-if="leftText.length"
                    >
                        <text :style="{ color: color, fontSize: '14px' }">{{ leftText }}</text>
                    </view>
                    <slot name="left" />
                </view>

                <view class="uni-navbar__header-container uni-navbar__content_view">
                    <view
                        class="uni-navbar__header-container-inner uni-navbar__content_view"
                        v-if="title.length"
                    >
                        <text class="uni-nav-bar-text" :style="{ color: color }">{{ title }}</text>
                    </view>
                    <!-- 标题插槽 -->
                    <slot />
                </view>

                <view
                    :class="title.length ? 'uni-navbar__header-btns-right' : ''"
                    @click="onClickRight"
                    class="uni-navbar__header-btns uni-navbar__content_view"
                >
                    <view class="uni-navbar__content_view" v-if="rightIcon.length">
                        <icons :color="color" :type="rightIcon" size="24" />
                    </view>
                    <view
                        class="uni-navbar-btn-text uni-navbar__content_view"
                        v-if="rightText.length && !rightIcon.length"
                    >
                        <text class="uni-nav-bar-right-text">{{ rightText }}</text>
                    </view>
                    <slot name="right" />
                </view>
            </view>
        </view>
        <view class="uni-navbar__placeholder" v-if="fixed">
            <view class="status-bar" />
            <view class="uni-navbar__placeholder-view" />
        </view>
    </view>
</template>

<script>
    import icons from "@/components/icons.vue";

    export default {
        name: "navBar",
        components: { icons },
        props: {
            title: {
                type: String,
                default: ""
            },
            leftText: {
                type: String,
                default: ""
            },
            rightText: {
                type: String,
                default: ""
            },
            leftIcon: {
                type: String,
                default: ""
            },
            rightIcon: {
                type: String,
                default: ""
            },
            fixed: {
                type: Boolean,
                default: true
            },
            color: {
                type: String,
                default: "#000000"
            },
            backgroundColor: {
                type: String,
                default: "transparent"
            },
            shadow: {
                type: String,
                default: ""
            },
            border: {
                type: [String, Boolean],
                default: true
            }
        },
        mounted() {
            if (uni.report && this.title !== "") {
                uni.report("title", this.title);
            }
        },
        methods: {
            onClickLeft() {
                this.$emit("tapLeft");
            },
            onClickRight() {
                this.$emit("tapRight");
            }
        }
    };
</script>

<style lang="scss" scoped>
    $nav-height: 44px;
    .uni-nav-bar-text {
        font-size: $uni-font-size-lg;
    }

    .uni-nav-bar-right-text {
        font-size: $uni-font-size-base;
    }

    .uni-navbar {
        width: 100vw;
    }

    .uni-navbar__content {
        position: relative;
        width: 100vw;
        background-color: $uni-bg-color;
        overflow: hidden;
    }

    .uni-navbar__content_view {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        align-items: center;
        flex-direction: row;
    }

    .uni-navbar__header {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        width: 100vw;
        height: $nav-height;
    }

    .uni-navbar__header-btns {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-wrap: nowrap;
        width: 120rpx;
        padding: 0 6px;
        justify-content: center;
        align-items: center;
    }

    .uni-navbar__header-btns-left {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        width: 150rpx;
        justify-content: flex-start;
    }

    .uni-navbar__header-btns-right {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        width: 150rpx;
        padding-right: 30rpx;
        justify-content: flex-end;
    }

    .uni-navbar__header-container {
        flex: 1;
    }

    .uni-navbar__header-container-inner {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex: 1;
        align-items: center;
        justify-content: center;
        font-size: $uni-font-size-base;
    }

    .uni-navbar--fixed {
        position: fixed;
        z-index: 998;
    }

    .uni-navbar--border {
        border-bottom-width: 1rpx;
        border-bottom-style: solid;
        border-bottom-color: $uni-border-color;
    }

    .uni-navbar__placeholder-view {
        height: $nav-height;
    }
</style>
